package com.own.business.login.core.core;

import com.own.business.login.common.property.LoginProperty;
import com.own.component.common.hook.BaseUserDetailHook;
import com.own.component.common.login.user.BaseLoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * UserSpringHook
 *
 * @author chenxueli
 * @date 2023-06-21 15:52:23
 */
@Slf4j
@Component
public class UserSpringHook {

    private static final Map<String, BaseUserDetailHook> USER_DETAIL_MANAGER = new LinkedHashMap<>(8);

    @Resource
    private LoginProperty loginProperty;
    @Resource
    private List<BaseUserDetailHook> baseUserDetailHookList;

    @PostConstruct
    private void init() {
        var array = loginProperty.getIgnoreUserDetailKeyArray();
        baseUserDetailHookList.forEach(service -> {
            var key = service.detailKey();
            if (key == null) {
                return;
            }
            log.debug("用户信息接口===>【{}:{}】完成注册", key, service.getClass().getName());
            if (array.contains(key)) {
                return;
            }
            USER_DETAIL_MANAGER.put(service.detailKey(), service);
        });
    }

    /**
     * 处理所有的获取用户信息的内容
     *
     * @param loginUser 登录的用户信息
     */
    public Map<String, Object> detail(BaseLoginUser loginUser) {
        var map = new HashMap<String, Object>(USER_DETAIL_MANAGER.size() * 2);
        USER_DETAIL_MANAGER.forEach((key, service) -> map.put(key, service.detail(loginUser)));
        return map;
    }

}
